﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.project.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.hh.bss.base.*;
import com.hh.bss.common.SystemConstant;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.config.Environment;
import com.hh.bss.project.dao.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.util.*;




@Service
@Transactional
public class ContractJobCvBackgroundServiceImpl extends BaseBusinessService<ContractJobCvBackground,java.lang.Integer>  implements ContractJobCvBackgroundService<ContractJobCvBackground,java.lang.Integer>{

	private ContractJobCvBackgroundDao contractJobCvBackgroundDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setContractJobCvBackgroundDaoImpl(ContractJobCvBackgroundDao dao) {
		this.contractJobCvBackgroundDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.contractJobCvBackgroundDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(ContractJobCvBackgroundQuery query) {
		return contractJobCvBackgroundDao.findPage(query);
	}
	
	public void saveFile(MultipartHttpServletRequest multipartRequest,
			HttpServletRequest request,
			ContractJobCvBackground contractJobCvBackground) {
		// TODO Auto-generated method stub
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");     
		  MultipartFile multipartFile = multipartRequest.getFile("fileRes");
	       /**构建文件保存的web目录**/     
	       
	       //把简历id也加到目录里
	       String fileWebPathDir = Environment.UPLOADFILEDIR+"/"+SystemConstant.JOBRECOMDIR+"/"+dateformat.format(new Date())+"/"+contractJobCvBackground.getRecomId();      
	       /**得到文件保存目录的真实路径**/     
	       String fileRealPathDir = request.getSession().getServletContext().getRealPath(fileWebPathDir);     
	       /**根据真实路径创建目录**/     
	       File fileSaveDir = new File(fileRealPathDir);     
	       if(!fileSaveDir.exists()) {    
	           fileSaveDir.mkdirs();     
	       }   
	       /**页面控件的文件流**/     
	       //MultipartFile multipartFile = multipartRequest.getFile("fileRes");      			       			      			       
	       /**上传文件名称**/     
	       String uploadFileName = multipartFile.getOriginalFilename();//    
	         
	       
	       /**获取文件的后缀**/     
	       String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".")+1);     
	       String preName = multipartFile.getOriginalFilename().substring(0,multipartFile.getOriginalFilename().indexOf("."));
	       // 文件类型   
	         
	       // 获取文件大小，以kB为单位   
	       long size = multipartFile.getSize();   
	       Double fSize =Math.round(size/1024.0*100)/100.0;   
	       String fileSize = Double.toString(fSize)+"KB";     
	       /**拼成完整的文件保存路径加文件**/     
	       String fileName = fileRealPathDir + File.separator   + uploadFileName;     
	       
	       File file = new File(fileName);      
	          
	       try {     
	           multipartFile.transferTo(file);  //将文件写入到指定的目录   
	       } catch (IllegalStateException e) {     
	           e.printStackTrace();     
	       } catch (IOException e) {            
	           e.printStackTrace();     
	       }    
	       
	       contractJobCvBackground.setFilename(fileWebPathDir+"/"+ uploadFileName);
	       //如果没有填文件名称，则以文件名处理
	       if (StringHelper.isEmpty(contractJobCvBackground.getDocName())) contractJobCvBackground.setDocName(preName);
	       
	       
	       contractJobCvBackground.setFilesize(fSize.intValue());
	       
		
	}
	
}
